﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace BioBooking.Data.CtrLag
{
    public class BookingController
    {
        BioBookingContainer container;
        public BookingController()
        {
            container = new BioBookingContainer();
        }

        public void CreateBooking(Showing show, List<Seat> seats, Member member, bool paymentReceived = false)
        {
            Booking booking = new Booking();
            booking.CreatedDate = DateTime.Now;
            booking.Member = member;
            booking.PaymentRecived = paymentReceived;
            booking.Showing = show;

            container.BookingSet.AddObject(booking);
            container.SaveChanges();
        }

        public void DeleteBooking(Booking booking)
        {
            container.BookingSet.DeleteObject(booking);
            container.SaveChanges();
        }

        public void UpdateBooking(Booking booking)
        {
            if (booking.EntityState == EntityState.Detached)
            {
                container.Attach(booking);
            }

            container.SaveChanges();
        }

        public bool ReserveSeats(List<Seat> seats, Showing showing)
        {
            //RefreshLockedSeats();

            container.Attach(showing);

            foreach (Seat seat in seats)
            {
                container.Attach(seat);

                SeatState state = new SeatState();
                state.Seat = seat;
                state.Showing = showing;
                state.Occupied = true;
                state.Locked = true;
                container.AddToSeatStateSet(state);
            }

            container.SaveChanges();
             
            return true;
        }

        public void PrintTicket(int bookingID)
        {
            Booking booking = container.BookingSet.FirstOrDefault(p => p.Id == bookingID);
            Console.WriteLine("Booking nr {0}", booking.Id);
            Console.WriteLine("Sal navn {0}", booking.Showing.Cinema.Name);
            Console.WriteLine("Siddeplads nr {0}", booking.Seats);
            Console.WriteLine("Medlem {0}",booking.Member.FullName);
            Console.WriteLine("Film set {0}", booking.Member.Counter);
            Console.WriteLine("Pris {0}", booking.Showing.UnitPrice);
            Console.WriteLine("Start tidspunkt {0}", booking.Showing.StartTime);
            Console.WriteLine("Slut tidspunkt {0}", booking.Showing.EndTime);
            Console.ReadLine();
        }
    }
}
